var express = require('express');
var BackstageUser = express.Router();
var dbcomm = require('./util/dbcomm');
var moment = require('moment');
var bodyparser = require('body-parser');
var now = moment().format('YYYY年MM月DD日') ;//日期

BackstageUser.use(bodyparser.urlencoded({extended:false}));


//添加用户
BackstageUser.get('/addUser',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "INSERT INTO `ourbarterterrace`.`sys_user` (userName,relName, userPass,userSex,userTelephone,userEmile,userProblemNo,userAnswer)"+
    " VALUES ('"+dates.userName+"', '"+dates.relName+"', '"+dates.userPass+"','"+dates.userSex+"','"+dates.userTelephone+"','"+dates.userEmile+"',"+dates.userProblemNo+",'"+dates.userAnswer+"')";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            if(data.affectedRows == 1){
                console.log("添加成功");
                res.redirect('/Backstage/frame/applymember.html');
            }else{
                console.log("添加失败");
                res.redirect('/Backstage/frame/adduser.html');
            }
            
        }
    });
});


//修改用户信息
BackstageUser.post('/updateuser',function(req,res){
    var dates = req.body;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_user` SET `userName`='"+dates.userName+"', `relName`='"+dates.relName+"',"+
    "  `userSex`='"+dates.userSex+"', `userTelephone`='"+dates.userTelephone+"', `userEmile`='"+dates.userEmile+"',"+
    "`userProblemNo`="+dates.userProblemNo+", `userAnswer`='"+dates.userAnswer+"' WHERE  `userId`="+dates.userId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                res.send(data);
            }else{
                res.send(data);
            }
            
        }
    });
});


//展示用户
BackstageUser.get('/showUser',function(req,res){
    var query = req.query;
    console.log(query);
    var star = parseInt(query.page - 1) * parseInt(query.rows);//query.page是当前页数,query.rows是每一页显示多少行数据
    var end = parseInt(query.rows);
    var sql = "select * from sys_user where userState = 1 limit ?,?";
    var myjson = {};
    dbcomm.query(sql,[star,end],function (err,data){
    //    if (err) throw err;
        // console.log(data);
        myjson.rows = data;//当前是多少行
        dbcomm.query("select * from sys_user where userState = 1 ",function (err,data){
            if (err) throw err;
            // console.log(data);
            myjson.total = data.length;//总共有多少行
            res.send(myjson);
        });
    });
});

//展示会员申请
BackstageUser.get('/showNewUser',function(req,res){
    var query = req.query;
    var star = parseInt(query.page - 1) * parseInt(query.rows);//query.page是当前页数,query.rows是每一页显示多少行数据
    var end = parseInt(query.rows);
    var sql = "select * from sys_user where userState = 0 or userState = 2 limit ?,?";
    var myjson = {};
    dbcomm.query(sql,[star,end],function (err,data){
    //    if (err) throw err;
        // console.log(data);
        myjson.rows = data;//当前是多少行
        dbcomm.query("select * from sys_user where userState = 0 or userState = 2 ",function (err,data){
            if (err) throw err;
            // console.log(data);
            myjson.total = data.length;//总共有多少行
            res.send(myjson);
        });
    });
});


//通过会员申请与解冻
BackstageUser.get('/adoptApply',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_user` SET `userState`= 1 WHERE  `userId`="+dates.userId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("审核通过");
            }else{
                console.log("审核失败，请重试");
            }
            
        }
    });
});

//冻结会员
BackstageUser.get('/frozeUser',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_user` SET `userState`= 2 WHERE  `userId`="+dates.userId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("审核通过");
            }else{
                console.log("审核失败，请重试");
            }
            
        }
    });
});



//作废会员
BackstageUser.get('/deleteUser',function(req,res){
    var dates = req.query;
    console.log(dates);
    // var sql = "delete from sys_user WHERE  `userId`="+dates.userId+";";
    var sql = "UPDATE `ourbarterterrace`.`sys_user` SET `userState`= 3 WHERE  `userId`="+dates.userId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("作废成功");
            }else{
                console.log("作废失败,请重试");
            }
            
        }
    });
});

//查找用户
BackstageUser.get('/findUserById',function(req,res){
    var dates = req.query;
    console.log("fsa");
    console.log(dates);
    var sql = "select userProblemNo,userState,userAnswer,userPass from sys_user where userId = "+dates.userId+"";
    dbcomm.query(sql,function (err,data){
            if (err) {throw err;}
            else{
                // console.log(data[0].userState);
                res.json(data[0]);
            }
    });
});



//修改用户密码
BackstageUser.get('/remUserPass',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "update sys_user set userPass ='"+dates.userPass+"' where userId = "+dates.userId+"";
    console.log(sql);
    dbcomm.query(sql,function (err,data){
            if (err) {throw err;}
            else{
                console.log(data);
                // res.json(data[0]);
                res.send(data);
            }
    });
});









module.exports = BackstageUser;